
/**   
*    
* ��Ŀ��ƣ�a_yongan   
* ����ƣ�UserController   
* ��������   
* �����ˣ�liubo   
* ����ʱ�䣺2018��5��31�� ����9:08:33    
* �޸�ʱ�䣺2018��5��31�� ����9:08:33   
* �޸ı�ע��   
* @version    
*    
*/
package com.yongan.user.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.user.dto.UserExecution;
import com.yongan.user.dto.UserExposer;
import com.yongan.user.dto.UserResult;
import com.yongan.user.entity.Dept;
import com.yongan.user.entity.User;
import com.yongan.user.service.UserService;
import com.yongan.user.util.StringToNumber;
import com.yongan.util.PageBean;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/{deptId}/usercontent", method = RequestMethod.GET)
	public String deptcontent(@PathVariable("deptId") String deptId, Model model,HttpServletRequest request) {
		List<User> userlist = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<Dept> pb = new PageBean<Dept>();
		if (deptId.equals("0")) {
			totalRecords = userService.getUserCount("", "");
		} else {
			totalRecords = userService.getCountByDeptId(deptId);
		}
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			if (deptId.equals("0")) {
				userlist = userService.getUserList(start, end);
			} else {
				userlist = userService.getUserListByDeptId(deptId, start, end);
			}
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("userlist", userlist);
		return "usercontent";
	}
	@RequestMapping(value="/userlist",method=RequestMethod.GET)
	public String userlist(Model model,HttpServletRequest request){
		String page = request.getParameter("currentPage");
		int currentPage;
		if (page!=null){
			currentPage = Integer.parseInt(page);
		}else{
			currentPage = 1;
		}
		PageBean<Dept> pb = new PageBean<Dept>();
		int totalRecords = userService.getUserCount("", "");
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords() / pb.getPagesize() + 1);
		if(totalRecords>0){
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			List<User> userlist = userService.getUserList(start, end);
			model.addAttribute("userlist", userlist);
			model.addAttribute("pb", pb);
		}
		return "userlist";
	}
	@RequestMapping(value="/{userId}/detail",method=RequestMethod.GET)
	public String userdetail(@PathVariable("userId") String userId,Model model){
		
		if(!userId.equals("")){
			User user = userService.getUserById(userId);
			model.addAttribute("userDetail", user);
		}
		return "userdetail";
	}
	@RequestMapping(value="/update",method=RequestMethod.POST,produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public UserResult<UserExecution> userupdate( User user,/*,HttpServletRequest request,@PathVariable("userId") String userId,*/Model model){
		System.out.println(user.toString()+"======");
		UserExecution rs = userService.executeUser(user, user.getUserId(), "123321231");
		int rsn = rs.getState();
		if(rsn==1){
			return new UserResult<UserExecution>(true, rs);
		}
		return new UserResult<UserExecution>(false, "");
	}
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String useradd(Model model){
		return "useradd";
	}
	@RequestMapping(value="/adddo",method=RequestMethod.POST,produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public UserResult<UserExecution> useradddo( User user,Model model){
		System.out.println(user.toString()+"======");
		UserExecution rs = userService.executeUser(user, "", "123321231");
		int rsn = rs.getState();
		if(rsn==1){
			return new UserResult<UserExecution>(true, rs);
		}
		return new UserResult<UserExecution>(false, "");
	}
	@RequestMapping(value="/{userId}/delete",method=RequestMethod.GET,produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public UserResult<UserExecution> userDelete(@PathVariable("userId") String userId,Model model){
		int ui = StringToNumber.stringToNumber(userId);
		System.out.println("��Ҫɾ����û���"+userId);
		if(ui!=0){
			UserExecution rsdelete = userService.executeUser(null, userId, "1212331232");
			int rsn = rsdelete.getState();
			if(rsn==1){
				return new UserResult<UserExecution>(true, rsdelete);
			}
		}
		return new UserResult<UserExecution>(false, "");
	}
	/*
	 * ��©�û������ӿ�
	 */
	@RequestMapping(value="/exposer",
			method=RequestMethod.GET,
			produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public UserResult<UserExposer> exposer(){
		System.out.println("=========");
		UserResult<UserExposer> result;
		try{
			//UserExposer exposer = userService.exportUserUrl(userId);
			result = new UserResult<UserExposer>(true, "");
			System.out.println("result:"+result.isSuccess());
		} catch (Exception e){
			logger.error(e.getMessage(),e);
			result = new UserResult<UserExposer>(false, e.getMessage());
		}
		return result;
	}
}
